package com.qf.hotel.dao.impl;

import com.qf.hotel.dao.FoodDao;
import com.qf.hotel.pojo.Food;
import com.qf.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Food持久层实现类
 */
public class FoodDaoImpl implements FoodDao {
    private QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
    /**
     * 查询菜品
     * @param food
     * @return
     * @throws SQLException
     */
    @Override
    public List<Map<String, Object>> findByCondition(Food food) throws SQLException {
        String sql = "SELECT\n" +
                "\tf.food_id AS foodId,\n" +
                "\tf.food_name as foodName,\n" +
                "\tf.food_price as foodPrice,\n" +
                "\tf.food_mprice as foodMprice,\n" +
                "\tf.food_image as foodImage,\n" +
                "\tf.food_desc as foodDesc,\n" +
                "\tft.type_id as typeId,\n" +
                "\tft.type_name as typeName \n" +
                "FROM\n" +
                "\tt_food f\n" +
                "\tINNER JOIN t_food_type ft ON f.type_id = ft.type_id WHERE f.food_name LIKE ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new MapListHandler(),food.getFoodName());
    }

    /**
     * 添加food
     * @param food
     * @return
     */
    @Override
    public int save(Food food) throws SQLException {
        String sql = "INSERT INTO t_food ( type_id, food_name, food_price, food_mprice, food_image, food_desc )\n" +
                "VALUES\n" +
                "\t( ?,?,?,?,?,?)";
        return queryRunner.update(JdbcUtils.getConnection(),sql,
                    food.getTypeId(),food.getFoodName(),food.getFoodPrice(),food.getFoodMprice(),food.getFoodImage(),food.getFoodDesc());
    }
}
